home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / tcoop.arc / TCOOP2.ARC / MSMOUSE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-26  |  7.1 KB  |  266 lines

  1. // Mouse class: msmouse.cpp 
  2.  
  3. #include "dos.h"
  4. #include "stdlib.h"
  5. #include "msmouse.h"
  6.  
  7. const int MsCall = 0x33;
  8. const int Iret   = 0xcf;
  9. const int False  = 0;
  10. const int True   = 1;
  11.  
  12. MouseObject Mouse;
  13.  
  14. MouseObject::MouseObject(void)
  15. // Initializes mouse to a known state. Mouse is not turned on yet 
  16. {
  17.   OK = False;
  18.   MouseOff = True;
  19. }
  20.  
  21. int MouseObject::DriverExists(void)
  22. // Returns true if a mouse driver is installed. This function makes
  23. // sure the interrupt vector location serviced by the mouse is
  24. // pointing to something--hopefully the mouse driver. 
  25. {
  26.   void far *address;
  27.   // Look for NULL address or IRET instruction
  28.   address = getvect(0x33);
  29.   return (address != NULL) && (*(unsigned char far *)address != Iret);
  30. }
  31.  
  32. void MouseObject::Setup(VideoModeType VideoMode)
  33. // Initializes the mouse object by verifying that the mouse driver
  34. // exists, that the mouse responds to its initialization function,
  35. // by moving the mouse to the top left of the screen, and by setting
  36. // various internal variables. Call the method SetupOK after
  37. // calling Init to find out if the mouse initialization was successful.
  38. // The VideoMode parameter specifies which video mode the mouse is
  39. // being used under. 
  40. {
  41.   REGS regs;
  42.  
  43.   OK = DriverExists();
  44.   if (OK) { // Mouse present, but will it reset? 
  45.      regs.x.ax = 0;
  46.      int86(MsCall, ®s, ®s);
  47.      if (regs.x.ax == 0) OK = False;
  48.   }
  49.   if (!OK)  {   // Mouse initialization failed. Set 
  50.      TurnOff(); // the mouse state off and return.  
  51.      return;
  52.   }
  53.   TurnOn();     // Set the mouse state on 
  54.  
  55.   if (VideoMode == TextScrn) TextMode = True; else TextMode = False;
  56.   if (VideoMode == LowResGr) LowRes = True; else LowRes = False;
  57.   // Fix up hercules mode for page 0. Use 5 for page 1. 
  58.   if (VideoMode == HerculesGr)  *(char far *)MK_FP(0x0040,0x0049) = 6;
  59.  
  60.   OldX = 0;  OldY = 0;   // Initialize various instance variables 
  61.   X = 0;     Y = 0;
  62.   Dx = 0;    Dy = 0;
  63.   Move(0,0); // Set the mouse to top,left of screen 
  64. }
  65.  
  66. int MouseObject::SetupOK(void)
  67. // Returns true only if the mouse initialization was successful 
  68. {
  69.   return OK;
  70. }
  71.  
  72. void MouseObject::Hide(void)
  73. // Hides the mouse cursor. Call this function to remove the mouse
  74. // cursor from the screen. 
  75. {
  76.   REGS regs;
  77.   if (!Operating()) return;
  78.   regs.x.ax = 2;
  79.   int86(MsCall, ®s, ®s);
  80. }
  81.  
  82. void MouseObject::Show(void)
  83. // Displays the mouse cursor 
  84. {
  85.   REGS regs;
  86.   if (!Operating()) return;
  87.   regs.x.ax = 1;
  88.   int86(MsCall, ®s, ®s);
  89. }
  90.  
  91. unsigned MouseObject::Status(int &Mx, int &My)
  92. // This general function returns the location of the mouse in Mx,My
  93. // and the current status of the buttons in the function name 
  94. {
  95.   REGS regs;
  96.  
  97.   if (!Operating()) { Mx = 0;  My = 0;  return 0; }
  98.   regs.x.ax = 3;
  99.   int86(MsCall, ®s, ®s);
  100.   Mx = regs.x.cx;   My = regs.x.dx;
  101.   if (TextMode)  {
  102.     Mx >>= 3;  // Adjust for text coordinates 
  103.     My >>= 3;
  104.   }
  105.   if (LowRes)  Mx >>= 1; // Adjust for 320x200 coordinates 
  106.   return regs.x.bx;
  107. }
  108.  
  109. unsigned MouseObject::ButtonStatus(void)
  110. // Returns the status of the mouse buttons 
  111. {
  112.   int Mx, My;
  113.   if (!Operating()) return 0; else return Status(Mx, My);
  114. }
  115.  
  116. int MouseObject::PressCnt(unsigned ButtonMask)
  117. // Returns number of times the button has been pressed since
  118. // last time called 
  119. {
  120.   REGS regs;
  121.   if (!Operating()) return 0;
  122.   regs.x.ax = 5;
  123.   regs.x.bx = ButtonMask >> 1; // Button selector 
  124.   int86(MsCall, ®s, ®s);
  125.   return regs.x.bx;
  126. }
  127.  
  128. int MouseObject::ReleaseCnt(unsigned ButtonMask)
  129. // Returns number of times the button has been released since
  130. // last time called 
  131. {
  132.   REGS regs;
  133.   if (!Operating()) return 0;
  134.   regs.x.ax = 6;
  135.   regs.x.bx = ButtonMask >> 1;     // Button selector 
  136.   int86(MsCall, ®s, ®s);
  137.   return regs.x.bx;
  138. }
  139.  
  140. unsigned MouseObject::Event(int &Mx, int &My)
  141. // Gets the last mouse event. The left mouse button has priority
  142. // over the right button. 
  143. {
  144.   unsigned E;
  145.  
  146.   if (!Operating()) { Mx = 0; My = 0; return Idle; }
  147.   // Get current status of buttons. 
  148.   E = Status(Mx,My);
  149.   if (E == 0) {
  150.     // No mouse button down, but maybe there was a button press that
  151.     // was missed. If not, check to see whether a button release was
  152.     // missed. Favor the left mouse button. 
  153.     if (PressCnt(LeftButton) > 0) E = LMouseDown;
  154.     else if (PressCnt(RightButton) > 0)  E = RMouseDown;
  155.     // Maybe left one was just released 
  156.     else if (ReleaseCnt(LeftButton) > 0) E = LMouseUp;
  157.     // Maybe right one was just released 
  158.     else if (ReleaseCnt(RightButton) > 0) E = RMouseUp;
  159.   }
  160.   else { // A mouse button is down 
  161.     // Was the left button already down? 
  162.     if (E & LeftButton) {
  163.       // Not already down 
  164.       if (PressCnt(LeftButton) > 0)
  165.          E = LMouseDown;            // Must have just been pressed 
  166.          else E = LMouseStillDown;  // Already down 
  167.     }
  168.     else if (PressCnt(RightButton) > 0)
  169.        E = RMouseDown;           // Must have just been pressed 
  170.        else E = RMouseStillDown; // Already down 
  171.   }
  172.   return E;     // Return the mouse event code 
  173. }
  174.  
  175. unsigned MouseObject::WaitForAnyEvent(int &Mx, int &My)
  176. // Waits for a mouse event to occur and return its code 
  177. {
  178.   unsigned E;
  179.  
  180.   if (!Operating()) { Mx = 0;  My = 0; return Idle; }
  181.   do {
  182.     E = Event(Mx, My);
  183.   } while (E == Idle);
  184.   return E;
  185. }
  186.  
  187. void MouseObject::WaitForEvent(unsigned E, int &Mx, int &My)
  188. // Waits for the event E to occur. Return its mouse coordinates. 
  189. {
  190.   unsigned Etry;
  191.   if (!Operating()) { Mx = 0; My = 0; return; }
  192.   do {
  193.     Etry = Event(Mx, My);
  194.   } while (Etry != E);
  195. }
  196.  
  197. int MouseObject::Moved(void)
  198. // Tests to see if the mouse has moved since the last time this
  199. // method was called. 
  200.   if (!Operating()) return False;
  201.   OldX = X; OldY = Y;
  202.   Status(X, Y);
  203.   Dx = X - OldX; Dy = Y - OldY;
  204.   return (Dx != 0) || (Dy != 0);
  205. }
  206.  
  207. void MouseObject::Move(int Mx, int My)
  208. // Moves the mouse cursor 
  209. {
  210.   REGS regs;
  211.  
  212.   if (!Operating()) return;
  213.   regs.x.ax = 4;
  214.   regs.x.cx = Mx;
  215.   regs.x.dx = My;
  216.   if (TextMode)  { // Adjust for text coordinates 
  217.     regs.x.cx <<= 3;
  218.     regs.x.dx <<= 3;
  219.   }
  220.   if (LowRes) regs.x.cx <<= 1;  // Adjust for 320x200 coordinates 
  221.   int86(MsCall, ®s, ®s);
  222. }
  223.  
  224. void MouseObject::TurnOn(void)
  225. // Enables the mouse code 
  226. {
  227.   if (OK && MouseOff) {
  228.       MouseOff = False;
  229.       Show();
  230.   }
  231. }
  232.  
  233. void MouseObject::TurnOff(void)
  234. // Disables the mouse code. This is useful when you don't want to
  235. // use the mouse, but the code already has mouse calls in it. 
  236. {
  237.   if (OK && !MouseOff) {
  238.     Hide();
  239.     MouseOff = True;
  240.   }
  241. }
  242.  
  243. int MouseObject::Operating(void)
  244. // Returns a boolean flag that is true only if the mouse 
  245. // object has been enabled. This is the default state. 
  246. {
  247.   return !MouseOff;
  248. }
  249.  
  250. void MouseObject::SetGCursor(const MouseCursor &NewCursor)
  251. // Sets the graphics mouse cursor to the type specified 
  252. {
  253.   REGS regs;
  254.   SREGS sregs;
  255.  
  256.   if (!Operating()) return;
  257.   regs.x.ax = 9;
  258.   regs.x.bx = NewCursor.HotSpot.X;
  259.   regs.x.cx = NewCursor.HotSpot.Y;
  260.   regs.x.dx = FP_OFF(NewCursor.ScreenMask);
  261.   sregs.es  = FP_SEG(NewCursor.ScreenMask);
  262.   int86x(MsCall, ®s, ®s, &sregs);
  263. }
  264.  
  265.